//
//  RJWarningVC.m
//  AirQuality
//
//  Created by Po on 15/3/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningVC.h"
#import "RJCityWarning.h"
#import "RJBuildingVC.h"
#import "CityWarningButton.h"
#import "RJHomePredictVC.h"
#import "RJProvinceForecastVC.h"
#import "ForecastMapController.h"

@interface RJWarningVC () <RJMainSuperDelegate>

@property (retain, nonatomic) NSArray * titleArray , * imageNames;
@property (retain, nonatomic) UIWebView * webView;

- (void)initInterface;
@end

@implementation RJWarningVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDelegate:self];
    [self.navBar.centerCityLabel setText:@"预报"];
    [self initInterface];
}

- (void)initInterface
{
//     _titleArray = @[@"预报首页",@"城市预报",@"全省预报",@"预报地图"];
    _titleArray = @[@"预报地图",@"预报发布",@"城市预报",@"全省预报"];
//    _imageNames = @[@"warning_Hut",@"warning_city2",@"warning_province2",@"warning_home2"];
    _imageNames = @[@"warning_home2",@"warning_Hut",@"warning_city2",@"warning_province2"];
//    _titleArray = @[@"预报主页",@"模式预报",@"城市预报",@"全省预报"];
    if (![RJUserData defaultRJUserData].isProvince) {
         _titleArray = @[@"预报地图",@"预报发布",@"城市预报"];
       _imageNames = @[@"warning_home2",@"warning_Hut",@"warning_city2"];
    }
   
//    NSArray * imageNames = @[@"warning_home",@"warning_pattern",@"warning_city",@"warning_province"];
    
    CGFloat  buttonSize = ((ScreenWidth) - 40);
    CGFloat  buttonHight = ScreenHeight/6;
    CGFloat x = 20;
    CGFloat y = 0.0;
    if ([RJUserData defaultRJUserData].isProvince) {
         y = buttonHight;
    }else{
         y = buttonHight/2*3;
    }
    for (NSUInteger i = 0; i < _titleArray.count; i ++) {
        CityWarningButton * button = [self createButtonWithFrame:CGRectMake(x, y, buttonSize, buttonHight) title:_titleArray[i] imageName:_imageNames[i]];
//        [button.titleLabel setFont:[UIFont systemFontOfSize:buttonSize / 10]];
        [self.view addSubview:button];
        y += buttonHight+6;
    }
    
    [self.backgroundImage setImage:GetImage(@"warningBack1.png")];
}

#pragma mark - function
- (CityWarningButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title imageName:(NSString *)imageName
{
    CityWarningButton * button = [[CityWarningButton alloc] initWithFrame:frame];
    [button.myTittleLabel setText:title];
//    [button.titleLabel setFont:[UIFont systemFontOfSize:(size - 2 * margin) / 5]];
//    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"homeTableCellBack"]]];
//    [button.layer setCornerRadius:5];
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//    [button setImageEdgeInsets:UIEdgeInsetsMake(30, 30, 30, 30)];
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    CGFloat width = frame.size.width;
    CGFloat height = frame.size.height;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake(width/2-30, height/2-30, 50, 50)];
    imageView.image = [UIImage imageNamed:imageName];
    [button addSubview:imageView];
    return button;
}

#pragma mark - event
- (void)pressButton:(CityWarningButton *)sender
{
    NSUInteger pageCount = [_titleArray indexOfObject:sender.myTittleLabel.text];
    switch (pageCount) {
        case 1:{
            RJHomePredictVC *homePredictVC = [[RJHomePredictVC alloc] init];
            [self.navigationController pushViewController:homePredictVC animated:YES];
        }
           break;
        case 3:{
            if ([RJUserData defaultRJUserData].isProvince) {
                RJProvinceForecastVC *provinceForecasstVC = [[RJProvinceForecastVC alloc] init];
                [self.navigationController pushViewController:provinceForecasstVC animated:YES];
            }else{
                ForecastMapController * forecastMap = [[ForecastMapController alloc] init];
                [self.navigationController pushViewController:forecastMap animated:YES];
            }
           
        }
            break;
        case 2:{
//            _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44)];
//            _webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
//            _webView.detectsPhoneNumbers = YES;
            RJCityWarning * cityWarning = [[RJCityWarning alloc] init];
            [self.navigationController pushViewController:cityWarning animated:YES];
//            NSURL *url =[NSURL URLWithString:@"http://newair.scemc.cn:5100/smartadmin/mobileForecastCity.html"];
//            NSURLRequest *request =[NSURLRequest requestWithURL:url];
//            [_webView loadRequest:request];
//            [_webView loadHTMLString: @"" baseURL: nil];
//            [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://newair.scemc.cn:5100/smartadmin/mobileForecastCity.html"]]];
//            [self.view addSubview:_webView];
            }
            break;
        case 0: {
            ForecastMapController * forecastMap = [[ForecastMapController alloc] init];
            [self.navigationController pushViewController:forecastMap animated:YES];
        }
            break;
        default:{
//            RJBuildingVC * buildingVC = [[RJBuildingVC alloc] init];
//            [self.navigationController pushViewController:buildingVC animated:YES];
//            _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
//            _webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
//            _webView.detectsPhoneNumbers = YES;
//            [_webView loadHTMLString: @"" baseURL: nil];
//            [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://newair.scemc.cn:5100/smartadmin/mobileForecastPro.html"]]];
//            [self.view addSubview:_webView];
            }
            break;
    }
}

- (void)RJMainSuperVCFreeAll
{
    
}
@end
